package com.hogan.basiclibrary.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

/**
 * Created by hogan on 17/5/10.
 * android自带的数据存储类
 */
public class SharedPreferencesUtils {

    public static final String SP_NAME = "hogan_base_sp";
    private Context mContext;
    private static SharedPreferencesUtils instance;

    public static SharedPreferencesUtils getInstance(Context context) {
        if (null == instance) {
            synchronized (SharedPreferencesUtils.class) {
                if (null == instance) {
                    instance = new SharedPreferencesUtils(context);
                }
            }
        }
        return instance;
    }


    public SharedPreferencesUtils(Context context) {
        this.mContext = context;
    }

    //默认
    @SuppressWarnings("unused")
    public void saveData(String key, Object object) {
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mContext.getApplicationContext());
        saveEditData(sp, key, object);
    }

    //自定义spname
    @SuppressWarnings("unused")
    public void saveData(String spname, String key, Object object) {
        SharedPreferences sp = mContext.getApplicationContext().getSharedPreferences(spname, Context.MODE_PRIVATE);
        saveEditData(sp, key, object);
    }

    private void saveEditData(SharedPreferences sp, String key, Object object) {
        SharedPreferences.Editor editor = sp.edit();
        if (object instanceof String) {
            editor.putString(key, (String) object);
        } else if (object instanceof Integer) {
            editor.putInt(key, (Integer) object);
        } else if (object instanceof Boolean) {
            editor.putBoolean(key, (Boolean) object);
        } else if (object instanceof Float) {
            editor.putFloat(key, (Float) object);
        } else if (object instanceof Long) {
            editor.putLong(key, (Long) object);
        }
        editor.apply();
    }

    public Object getData(String spName, String key,
                          Object defaultObject) {
        SharedPreferences sp = mContext.getApplicationContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
        return getEditData(sp, key, defaultObject);
    }

    @SuppressWarnings("unused")
    public Object getData(String key,
                          Object defaultObject) {
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mContext.getApplicationContext());
        return getEditData(sp, key, defaultObject);
    }

    private Object getEditData(SharedPreferences sp, String key, Object defaultObject) {
        if (defaultObject instanceof String) {
            return sp.getString(key, (String) defaultObject);
        } else if (defaultObject instanceof Integer) {
            return sp.getInt(key, (Integer) defaultObject);
        } else if (defaultObject instanceof Boolean) {
            return sp.getBoolean(key, (Boolean) defaultObject);
        } else if (defaultObject instanceof Float) {
            return sp.getFloat(key, (Float) defaultObject);
        } else if (defaultObject instanceof Long) {
            return sp.getLong(key, (Long) defaultObject);
        } else {
            throw new RuntimeException("只支持基本类型");
        }
    }

    @SuppressWarnings("unused")
    public void removeData(String spName, String key) {   //移除指定sp中的指定元素
        SharedPreferences sp = mContext.getApplicationContext().getSharedPreferences(spName, Context.MODE_PRIVATE);
        sp.edit().remove(key).apply();
    }

    @SuppressWarnings("unused")
    public void removeData(String key) {   //移除sp中的指定元素
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mContext.getApplicationContext());
        sp.edit().remove(key).apply();
    }

}
